<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner_model extends OC_Model {

    function get_all($params = array()) {

        $this->db->select('SQL_CALC_FOUND_ROWS *', FALSE);
        $this->db->from('banners');
        $query = $this->db->get();
        
        if (isset($params['sort'], $params['order'])) $this->db->order_by($params['sort'], $params['order']);
        
        if (isset($params['page'], $params['rows'])) $this->db->limit($params['rows'], (intval($params['page'] - 1) * intval($params['rows'])));
        
        if (isset($params['query']) && $params['query'] != '') {
            $query = $params['query'];
            
            $likes = array_map(function ($f) use ($query) {
                return "$f LIKE '%$query%'";
            }
            , array(
                'b_name',
                'b_origname',
                'b_content',
                'b_url'
            ));
            
            $this->db->where('(' . implode(' OR ', $likes) . ')');
        }
        
        if (isset($params['b_id']) && $params['b_id'] != '') $this->db->where('b_id', $params['b_id']);
        
        if ($query->num_rows() > 0) return $query->result();
        
        return array();
    }
    
    function get_by_id($banners_id) {
        $this->db->where('b_id', $banners_id);
        $qry = $this->db->get('banners');
        
        if ($qry->num_rows() > 0) {
            return $qry->row();
        }
        
        return NULL;
    }
    
    function get_images($banners_id) {
        $this->db->from('banners');
        $this->db->where('b_id', $banners_id);
        
        return $this->get_rows();
    }
    
    function add($data) {
        return $this->db->insert('banners', $data);
    }
    
    function edit($data, $keys) {
        return $this->db->update('banners', $data, $keys);
    }
    
    function delete($keys) {
        return $this->db->delete('banners', $keys);
    }

}

/* End of file main_banner_model.php */
/* Location: .//D/private/ia-pkm/dnetnorfintek/module_admin/banners/models/main_banner_model.php */